package com.hengtongxing.hejiayun_employee.homepage.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hengtongxing.hejiayun_employee.R;
import com.hengtongxing.hejiayun_employee.bean.TaskListBean;
import com.hengtongxing.hejiayun_employee.homepage.BigImageActivity;
import com.hengtongxing.hejiayun_employee.utils.DateTimeUtil;
import com.hengtongxing.hejiayun_employee.utils.Dp2Px;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: HWC
 * Description: 订单列表
 */
public class HousekeepListAdapter extends RecyclerView.Adapter<HousekeepListAdapter.ViewHolder> {
    private RecyclerView.ItemDecoration itemDecoration;
    private List<TaskListBean.DataBean.ItemListBean> listsBeans;
    private Context context;

    public HousekeepListAdapter(List<TaskListBean.DataBean.ItemListBean> listsBeans) {
        this.listsBeans = listsBeans;
        itemDecoration = getItemDecoration();
    }

    private OnCallBackListener onCallBackListener;

    public void setOnCallBackListener(OnCallBackListener onCallBackListener) {
        this.onCallBackListener = onCallBackListener;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        context = viewGroup.getContext();
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_housekeeping_list, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(inflate);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TaskListBean.DataBean.ItemListBean dataBean = listsBeans.get(position);
        holder.tvCreateTime.setText(DateTimeUtil.format7(new Date(Long.parseLong(dataBean.getCreate_time()) * 1000)));
        String address = dataBean.getAddress().getComm_name() + dataBean.getAddress().getBuilding() + dataBean.getAddress().getUnit_name() + dataBean.getAddress().getFloor() + dataBean.getAddress().getHose_number();
        holder.tvHouse.setText(address);
        holder.tvInfo.setText(dataBean.getInfo());
        holder.tvVisitTime.setText("期望上门时段：" + dataBean.getVisit_time());
        holder.tvName.setText(dataBean.getName());
        holder.tvMobile.setText(dataBean.getMobile());

        holder.recyclerViewPic.setLayoutManager(new GridLayoutManager(context, 3));
        DynamicPicAdapter adapter = new DynamicPicAdapter(context, dataBean.getImg());
        holder.recyclerViewPic.setAdapter(adapter);
        holder.recyclerViewPic.removeItemDecoration(itemDecoration);
        holder.recyclerViewPic.addItemDecoration(itemDecoration);
        adapter.setListener(position1 -> {
            Intent intent = new Intent(context, BigImageActivity.class);
            intent.putExtra("list", (Serializable) dataBean.getImg());
            intent.putExtra("position", position1);
            context.startActivity(intent);
        });
        holder.tvCommit.setVisibility(View.GONE);
        // 1待接单(已下单) 2已接单(待解决) 3已维修(待付款) 4已完成
        holder.llMoney.setVisibility(View.GONE);
        if (dataBean.getStatus() == 1) {
            holder.tvCommit.setText("确认接单");
            holder.tvCommit.setVisibility(View.VISIBLE);
        } else if (dataBean.getStatus() == 2) {
            holder.llMoney.setVisibility(View.VISIBLE);
            holder.tvCommit.setText("确认解决");
            holder.tvCommit.setVisibility(View.VISIBLE);
        } else if (dataBean.getStatus() == 3) {
            holder.tvCommit.setText("等待付款");
            holder.tvCommit.setVisibility(View.VISIBLE);
        } else if (dataBean.getStatus() == 4) {
            holder.tvCommit.setText("已完成");
            holder.tvCommit.setVisibility(View.GONE);
        } else {
            holder.tvCommit.setVisibility(View.GONE);
        }
        holder.llTelephone.setOnClickListener(view -> {
            onCallBackListener.onCallBack(1, position);
        });
        holder.tvCommit.setOnClickListener(view -> {
            onCallBackListener.onCallBack(2, position);
        });
    }

    private RecyclerView.ItemDecoration getItemDecoration() {
        return new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
                    state) {
                GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();
                int spanSize = layoutParams.getSpanSize();
                int spanIndex = layoutParams.getSpanIndex();
                outRect.bottom = (int) Dp2Px.dp2Px(10);
                if (spanSize != 3) {
                    switch (spanIndex) {
                        case 0:
                            outRect.left = (int) Dp2Px.dp2Px(0);
                            outRect.right = (int) Dp2Px.dp2Px(7);
                            break;
                        case 1:
                            outRect.left = (int) Dp2Px.dp2Px(3);
                            outRect.right = (int) Dp2Px.dp2Px(3);
                            break;
                        case 2:
                            outRect.left = (int) Dp2Px.dp2Px(7);
                            outRect.right = (int) Dp2Px.dp2Px(0);
                            break;
                    }
                }
            }
        };

    }

    @Override
    public int getItemCount() {
        return listsBeans.size() == 0 ? 0 : listsBeans.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_create_time)
        TextView tvCreateTime;
        @BindView(R.id.tv_visit_time)
        TextView tvVisitTime;
        @BindView(R.id.ll_telephone)
        LinearLayout llTelephone;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_mobile)
        TextView tvMobile;
        @BindView(R.id.tv_info)
        TextView tvInfo;
        @BindView(R.id.recyclerView_pic)
        RecyclerView recyclerViewPic;
        @BindView(R.id.tv_house)
        TextView tvHouse;
        @BindView(R.id.tv_commit)
        TextView tvCommit;
        @BindView(R.id.ll_money)
        LinearLayout llMoney;
        @BindView(R.id.edit_money)
        EditText editMoney;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            editMoney.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    listsBeans.get(getAdapterPosition()).setMoney(s.toString());
                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
    }

    public interface OnCallBackListener {
        void onCallBack(int type, int position);
    }
}
